package com.fs.groupSeven.controller;

import com.fs.groupSeven.pojo.Role;
import com.fs.groupSeven.pojo.User;
import com.fs.groupSeven.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author: st
 * @createDate: 2020/10/2
 * @version: 1.0
 */

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(String username, String password, String rememberMe, HttpServletResponse response,HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        loginOut();


        if (username != null && !username.equals("")
                && password != null && !password.equals("")) {
            if (rememberMe != null && rememberMe.equals("true")) {
                Cookie c1 = new Cookie("USERNAME",username);
                Cookie c2 = new Cookie("PASSWORD", password);
                c1.setMaxAge(30 * 24 * 60 * 60);
                c2.setMaxAge(30 * 24 * 60 * 60);
                response.addCookie(c1);
                response.addCookie(c2);
            }
            if (rememberMe != null && rememberMe.equals("false")){
                Cookie[] cookies = request.getCookies();
                if (cookies != null) {
                    for (Cookie c : cookies) {
                        if ("USERNAME".equals(c.getName())) {
                            c.setMaxAge(0);
                            response.addCookie(c);
                        }
                        if ("PASSWORD".equals(c.getName())) {
                            c.setMaxAge(0);
                            response.addCookie(c);
                        }
                    }
                }
            }

            return userService.login(username, password);
        }
        result.put("code", -10);
        result.put("message", "登录异常");
        return result;
    }

    public void loginOut() {
        User user = userService.loginOut();
    }

    @RequestMapping("/rememberMe")
    @ResponseBody
    public Map<String, Object> rememberMe(HttpServletRequest req) {
        Map<String, Object> result = new HashMap<>();
        Cookie[] cookies = req.getCookies();

        if (cookies != null) {
            for (Cookie c : cookies) {
                if ("USERNAME".equals(c.getName())) {
                    String username = c.getValue();
                    result.put("username", username);
                }
                if ("PASSWORD".equals(c.getName())) {
                    String password = c.getValue();
                    result.put("password", password);
                }
            }
            return result;
        }
        return result;
    }

    @RequestMapping("/getPassword")
    @ResponseBody
    public Map<String, Object> getPassword(String username) {
        Map<String, Object> result = new HashMap<>();

        if (username != null && !username.equals("")) {
            User user = userService.getOneByUsername(username);
            result.put("code", 0);
            result.put("user", user);
            return result;
        }
        result.put("code", -10);
        result.put("message", "异常");
        return result;
    }

    @RequestMapping("/updatePassword")
    @ResponseBody
    public Map<String, Object> updatePassword(String username, String password) {
        Map<String, Object> result = new HashMap<>();
        User user = new User();

        if (username != null && !username.equals("")
                && password != null && !password.equals("")) {
            user.setUsername(username);
            user.setPassword(password);
            userService.modifyOne(user);
        }
        result.put("code", -10);
        result.put("message", "异常");
        return result;
    }

    @RequestMapping("/addOne")
    @ResponseBody
    public Map<String, Object> addOne(String username, String password,String name,String roleString) {
        Map<String, Object> result = new HashMap<>();
        Set<Role> roles = new HashSet<>();
        User user = new User();
        Role role = new Role();

        if (username != null && !username.equals("")
                && password != null && !password.equals("")
                && roleString != null && !roleString.equals("")) {
            role.setRoleId(Integer.parseInt(roleString));
            roles.add(role);
            user.setUsername(username);
            user.setPassword(password);
            user.setName(name);
            user.setRoles(roles);
            user.setState(1);
            userService.addOne(user);
        }
        result.put("code", -10);
        result.put("message", "异常");
        return result;
    }
}
